package com.vita.bduck.common;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.SystemClock;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * android 系统信息的工具包
 * Created with IntelliJ IDEA.
 * User: Vita Hao
 * Date: 13-6-19
 * Time: 下午3:20
 * To change this template use File | Settings | File Templates.
 */
public class SystemUtils {


    /**
     * CPU信息
     * //proc//cpuinfo文件中第一行是CPU的型号，第二行是CPU的频率
     *
     * @return
     */
    public String[] getCpuInfo() {
        String str1 = "/proc/cpuinfo";
        String str2 = "";
        String[] cpuInfo = {"", ""};
        String[] arrayOfString;
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            for (int i = 2; i < arrayOfString.length; i++) {
                cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
            }
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            cpuInfo[1] += arrayOfString[2];
            localBufferedReader.close();
        } catch (IOException e) {

        }
        return cpuInfo;
    }

    /**
     * 系统的版本信息
     * 版本信息里面还包括型号等信息。
     *
     * @return
     */
    public String[] getVersion() {
        String[] version = {"null", "null", "null", "null"};
        String str1 = "/proc/version";
        String str2;
        String[] arrayOfString;
        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(
                    localFileReader, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            version[0] = arrayOfString[2];//KernelVersion
            localBufferedReader.close();
        } catch (IOException e) {
        }
        version[1] = Build.VERSION.RELEASE;// firmware version
        version[2] = Build.MODEL;//model
        version[3] = Build.DISPLAY;//system version
        return version;
    }

    public static int getAndroidSDKVersion() {
        int version = 0;
        try {
            version = Integer.valueOf(Build.VERSION.SDK_INT);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return version;
    }

    /**
     * Cupcake (android 1.5)
     * @return
     */
    public static boolean hasCupcake(){
        if(getAndroidSDKVersion() >= Build.VERSION_CODES.CUPCAKE){
            return true;
        }
        return false;
    }

    /**
     * Donut(android 1.6)
     * @return
     */
    public static boolean hasDonut(){
        if(getAndroidSDKVersion() >= Build.VERSION_CODES.DONUT){
            return true;
        }
        return false;
    }

    /**
     * Froyo（android 2.2）
     * @return
     */
    public static boolean hasFroyo(){
        if(getAndroidSDKVersion() >= Build.VERSION_CODES.FROYO){
            return true;
        }
        return false;
    }

    /**
     *  Gingerbread (android 2.3)
     * @return
     */
    public static boolean hasGingerbread(){
        if(getAndroidSDKVersion() >= Build.VERSION_CODES.GINGERBREAD){
            return true;
        }
        return false;
    }

    /**
     * Honeycomb (android 3.0)
     * @return
     */
    public static boolean hasHoneycomb(){
        if(getAndroidSDKVersion() >= Build.VERSION_CODES.HONEYCOMB){
            return true;
        }
        return false;
    }

    /**
     * IceCreamSandwich(android 4.0)
     * @return
     */
    public static boolean hasIceCreamSandwich(){
        if(getAndroidSDKVersion() >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            return true;
        }
        return false;
    }

    /**
     * 获取开机时间
     * @return
     */
    public static String getTimes(Context mContext) {
        long ut = SystemClock.elapsedRealtime() / 1000;
        if (ut == 0) {
            ut = 1;
        }
        int m = (int) ((ut / 60) % 60);
        int h = (int) ((ut / 3600));
        int s = (int) ((ut % 3600) % 60);
        return h + ":"+ m + ":" + s;
    }
}
